package ronin.fx;

import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import ronin.graphics.*;
import ronin.actor2d.*;
import ronin.util.*;
import ronin.fx.*;

// TODO

// This needs to be changed so that it can be attached to a host

public class PSSmokeTrail extends ParticleSystem
{
	public PSSmokeTrail(Vector2D pos, ParticleManager m)
	{
		super(pos, m);
		
		defInitInfo = new PSInitInfo();
		defInitInfo.type = ParticleSystem.PS_SMOKE_TRAIL;
		defInitInfo.maxParticles = 100;
		defInitInfo.shape = new Rectangle2D.Double(0.0, 0.0, 5.0, 5.0);
		defInitInfo.texture = null;
		defInitInfo.textureFile = "";
		defInitInfo.alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.15f);
		defInitInfo.bounds = new Rectangle2D.Double(0.0, 0.0, 20.0, 20.0);
		
		forces = new Vector2D.Double(0.0, 0.0);
		
		setupSystem(defInitInfo);
		startParticle(10);
	}
	
	
	
	public PSSmokeTrail(Actor2D objectAttachedTo, Vector2D newPos, ParticleManager m)
	{
		super(objectAttachedTo, newPos, m);
		
		defInitInfo = new PSInitInfo();
		defInitInfo.type = ParticleSystem.PS_SMOKE_TRAIL;
		defInitInfo.maxParticles = 100;
		defInitInfo.shape = new Rectangle2D.Double(0.0, 0.0, 5.0, 5.0);
		defInitInfo.texture = null;
		defInitInfo.textureFile = "";
		defInitInfo.alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.15f);
		defInitInfo.bounds = new Rectangle2D.Double(0.0, 0.0, 20.0, 20.0);
		
		forces = new Vector2D.Double(0.0, 0.0);
		
		setupSystem(defInitInfo);
		startParticle(10);
	}
	
	
	public void startParticle(int n)
	{
		if(n <= 0) return; // i must be greater than 0
		
		Particle p;
		
		double rand, r, s;
		
		for(int i=0; i<n; i++)
		{
			rand = 20 + (Math.random()*20);
			r = -0.5 + (Math.random()*1);// yfactor
			s = 1 + (Math.random()*5);
			
			p = (Particle) manager.particlePool.removeLast();
			p.energy	= (int) rand;
			p.pos 		= new Vector2D.Double(pos.getX(), pos.getY());
			
		/*	if(leach)
			{
				// setup p.xvel
				if(host.vel.getX() >0) 
				{
					p.vel.setX(-s + r);
				}
				else 
				{ 
					p.vel.setX(s + r);
				}
				
				// p.yvel
				if(host.vel.getY()>0)
				{
					p.vel.setY
			}
		*/
			p.vel 		= new Vector2D.Double(-s, r);
			p.color 	= Color.gray;
			p.size		= 1; 
			
			particles.add(p);
		}
	}
	
	
	public void update()
	{
		super.update();
		
		Particle p;
		
		for(int i=0; i<particles.size(); i++)
		{
			p =(Particle) particles.get(i);
			
			if(p.energy > 0)
			{
				p.energy--;
				p.pos.translate(p.vel);
				p.vel.translate(forces);
				p.color.darker();
			}
			else
			{
				releaseParticle(i);
			}
		}
		startParticle(10);
	}
	
	
	public void paint(Graphics2D g2d)
	{
		Particle p;
		
		g2d.setComposite(alpha);
		
		for(int i=0; i<particles.size(); i++)
		{
			p = (Particle) particles.get(i);
			
			g2d.setTransform(new AffineTransform());
			g2d.translate(p.pos.getX(), p.pos.getY());
			g2d.scale(p.size, p.size);
			g2d.setPaint(p.color);
			g2d.fill(shape);
		}	
	}
}